﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Petra.Gis.Geometry.Image.Tiff
{
  public static class TiffColorSpaceConverter
  {

    private delegate byte[] GetPixel(BlockBitmapFrame image);


    static TiffColorSpaceConverter()
    {

    }




    private static bool isColorSpaceConverterNeeded(TiffMetadata metadata, PixelFormat destPixelFormat)
    {


      return true;
    }

    public static byte[] Convert(BitmapBlock block, TiffPixelFormat tiffSrcPixelFormat, PixelFormat destPixelFormat)
    {

      if ((destPixelFormat != PixelFormats.Rgb24) || (destPixelFormat.BitsPerPixel < 8) || (destPixelFormat.BitsPerPixel % 8 != 0))
        throw new ArgumentException("unsupported bitsPerPixel, must be >= 8 and modulo 8");

      //if(!isColorSpaceConverterNeeded(metadata, destPixelFormat))
      //  return block.
      //design destination bitmap
      byte[] destBitmap = new byte[BitmapBlock.GetStrideLenght(block.Bounds.Width, destPixelFormat.BitsPerPixel) * block.Bounds.Height];



      if (tiffSrcPixelFormat == TiffPixelFormats.PlanarYCbCr24bpp)
      {
        //SeparatedBitmapBlock b = (SeparatedBitmapBlock)block;
        PCDYCbCrConversion.convertYccToRgb((SeparatedBitmapBlock)block, destBitmap);
      }



      return destBitmap;

    }



  }
}
